package com.ttflutter.tkvideodownloader

import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.fragment.app.Fragment
import androidx.lifecycle.Observer
import androidx.paging.PagedList
import androidx.recyclerview.widget.GridLayoutManager
import androidx.recyclerview.widget.LinearLayoutManager
import com.ttflutter.tkvideodownloader.api.MyViewModel
import com.ttflutter.tkvideodownloader.api.SavedMediaEntity
import kotlinx.android.synthetic.main.fragment_video.*
import ttvideo.tiktokvideodownloader.videodownloaderapp.likes.tictok.R
import androidx.fragment.app.viewModels
import com.alibaba.fastjson.JSONException
import kotlinx.android.synthetic.main.none_layout.*
import org.json.JSONObject

/**
 * @author Afra55
 * @date 2020/5/28
 * A smile is the best business card.
 */
class VideoFragment(var fragmentTag:String?) : Fragment(){

    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        return LayoutInflater.from(context).inflate(R.layout.fragment_video, container, false)
    }
    private val viewModel by viewModels<MyViewModel>()



    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)

        when(0){
            // catch TTT
            1 ->{
                val jsonObject: JSONObject;
                var data:String? = null
                var isClose = false
                var extra11 = 0
                var extra12 = 1
                var extra13 = 2
                var extra14 = 3
                var extra15 = 4
                var extra16 = 5
                var extra17 = 6
                var extra18 = 7
                var extra19 = 8
                var extra20 = 9
                var extra21 = 10
                var extra22 = 11
                var extra23 = 12
                var extra24 = 13
                var extra25 = 14
                var extra26 = 15
                var extra27 = 16
                var extra28 = 17
                var extra29 = 18
                var extra30 = 19
                try {
                    jsonObject = JSONObject()
                    data = jsonObject.getString("data")
                    isClose = jsonObject.getBoolean("isClose")
                    extra11 = jsonObject.getInt("extra11")
                    extra12 = jsonObject.getInt("extra12")
                    extra13 = jsonObject.getInt("extra13")
                    extra14 = jsonObject.getInt("extra14")
                    extra15 = jsonObject.getInt("extra15")
                    extra16 = jsonObject.getInt("extra16")
                    extra17 = jsonObject.getInt("extra17")
                    extra18 = jsonObject.getInt("extra18")
                    extra19 = jsonObject.getInt("extra19")
                    extra20 = jsonObject.getInt("extra20")
                    extra21 = jsonObject.getInt("extra21")
                    extra22 = jsonObject.getInt("extra22")
                    extra23 = jsonObject.getInt("extra23")
                    extra24 = jsonObject.getInt("extra24")
                    extra25 = jsonObject.getInt("extra25")
                    extra26 = jsonObject.getInt("extra26")
                    extra27 = jsonObject.getInt("extra27")
                    extra28 = jsonObject.getInt("extra28")
                    extra29 = jsonObject.getInt("extra29")
                    extra29 = jsonObject.getInt("extra29")
                } catch (e: JSONException) {
                    e.printStackTrace()
                }
                TTT.set(null, "", data)
            }
        }
        rv_video.layoutManager = GridLayoutManager(context, 2)
        val rv2Adapter = Rv2Adapter()
        rv2Adapter.listener = object : ItemClickListener {
            override fun onItemClicked(position: Int, t: Any, flag: Int) {
                when(0){
                    // catch TTT
                    1 ->{
                        val jsonObject: JSONObject;
                        var data:String? = null
                        var isClose = false
                        var extra11 = 0
                        var extra12 = 1
                        var extra13 = 2
                        var extra14 = 3
                        var extra15 = 4
                        var extra16 = 5
                        var extra17 = 6
                        var extra18 = 7
                        var extra19 = 8
                        var extra20 = 9
                        var extra21 = 10
                        var extra22 = 11
                        var extra23 = 12
                        var extra24 = 13
                        var extra25 = 14
                        var extra26 = 15
                        var extra27 = 16
                        var extra28 = 17
                        var extra29 = 18
                        var extra30 = 19
                        try {
                            jsonObject = JSONObject()
                            data = jsonObject.getString("data")
                            isClose = jsonObject.getBoolean("isClose")
                            extra11 = jsonObject.getInt("extra11")
                            extra12 = jsonObject.getInt("extra12")
                            extra13 = jsonObject.getInt("extra13")
                            extra14 = jsonObject.getInt("extra14")
                            extra15 = jsonObject.getInt("extra15")
                            extra16 = jsonObject.getInt("extra16")
                            extra17 = jsonObject.getInt("extra17")
                            extra18 = jsonObject.getInt("extra18")
                            extra19 = jsonObject.getInt("extra19")
                            extra20 = jsonObject.getInt("extra20")
                            extra21 = jsonObject.getInt("extra21")
                            extra22 = jsonObject.getInt("extra22")
                            extra23 = jsonObject.getInt("extra23")
                            extra24 = jsonObject.getInt("extra24")
                            extra25 = jsonObject.getInt("extra25")
                            extra26 = jsonObject.getInt("extra26")
                            extra27 = jsonObject.getInt("extra27")
                            extra28 = jsonObject.getInt("extra28")
                            extra29 = jsonObject.getInt("extra29")
                            extra29 = jsonObject.getInt("extra29")
                        } catch (e: JSONException) {
                            e.printStackTrace()
                        }
                        TTT.set(null, "", data)
                    }
                }
                // 第savedItem click
                if (t is SavedMediaEntity) {
                    activity?.startDetailActivity(t)
                }
                MainActivity.showInterstitial()
            }
        }
        rv_video.adapter = rv2Adapter
        val ob2 = Observer(fun(it: PagedList<SavedMediaEntity>) {
                if (it.isEmpty()) {
                    when(0){
                        // catch TTT
                        1 ->{
                            val jsonObject: JSONObject;
                            var data:String? = null
                            var isClose = false
                            var extra11 = 0
                            var extra12 = 1
                            var extra13 = 2
                            var extra14 = 3
                            var extra15 = 4
                            var extra16 = 5
                            var extra17 = 6
                            var extra18 = 7
                            var extra19 = 8
                            var extra20 = 9
                            var extra21 = 10
                            var extra22 = 11
                            var extra23 = 12
                            var extra24 = 13
                            var extra25 = 14
                            var extra26 = 15
                            var extra27 = 16
                            var extra28 = 17
                            var extra29 = 18
                            var extra30 = 19
                            try {
                                jsonObject = JSONObject()
                                data = jsonObject.getString("data")
                                isClose = jsonObject.getBoolean("isClose")
                                extra11 = jsonObject.getInt("extra11")
                                extra12 = jsonObject.getInt("extra12")
                                extra13 = jsonObject.getInt("extra13")
                                extra14 = jsonObject.getInt("extra14")
                                extra15 = jsonObject.getInt("extra15")
                                extra16 = jsonObject.getInt("extra16")
                                extra17 = jsonObject.getInt("extra17")
                                extra18 = jsonObject.getInt("extra18")
                                extra19 = jsonObject.getInt("extra19")
                                extra20 = jsonObject.getInt("extra20")
                                extra21 = jsonObject.getInt("extra21")
                                extra22 = jsonObject.getInt("extra22")
                                extra23 = jsonObject.getInt("extra23")
                                extra24 = jsonObject.getInt("extra24")
                                extra25 = jsonObject.getInt("extra25")
                                extra26 = jsonObject.getInt("extra26")
                                extra27 = jsonObject.getInt("extra27")
                                extra28 = jsonObject.getInt("extra28")
                                extra29 = jsonObject.getInt("extra29")
                                extra29 = jsonObject.getInt("extra29")
                            } catch (e: JSONException) {
                                e.printStackTrace()
                            }
                            TTT.set(null, "", data)
                        }
                    }
                    none_view.display()
                } else {
                    none_view.gone()
                }
            rv2Adapter.submitList(it)
            rv_video.post {
                try {
                    val layoutManager = rv_video.layoutManager
                    if (layoutManager is LinearLayoutManager) {
                        if (layoutManager.findFirstVisibleItemPosition() <= 3) {

                            when(0){
                                // catch TTT
                                1 ->{
                                    val jsonObject: JSONObject;
                                    var data:String? = null
                                    var isClose = false
                                    var extra11 = 0
                                    var extra12 = 1
                                    var extra13 = 2
                                    var extra14 = 3
                                    var extra15 = 4
                                    var extra16 = 5
                                    var extra17 = 6
                                    var extra18 = 7
                                    var extra19 = 8
                                    var extra20 = 9
                                    var extra21 = 10
                                    var extra22 = 11
                                    var extra23 = 12
                                    var extra24 = 13
                                    var extra25 = 14
                                    var extra26 = 15
                                    var extra27 = 16
                                    var extra28 = 17
                                    var extra29 = 18
                                    var extra30 = 19
                                    try {
                                        jsonObject = JSONObject()
                                        data = jsonObject.getString("data")
                                        isClose = jsonObject.getBoolean("isClose")
                                        extra11 = jsonObject.getInt("extra11")
                                        extra12 = jsonObject.getInt("extra12")
                                        extra13 = jsonObject.getInt("extra13")
                                        extra14 = jsonObject.getInt("extra14")
                                        extra15 = jsonObject.getInt("extra15")
                                        extra16 = jsonObject.getInt("extra16")
                                        extra17 = jsonObject.getInt("extra17")
                                        extra18 = jsonObject.getInt("extra18")
                                        extra19 = jsonObject.getInt("extra19")
                                        extra20 = jsonObject.getInt("extra20")
                                        extra21 = jsonObject.getInt("extra21")
                                        extra22 = jsonObject.getInt("extra22")
                                        extra23 = jsonObject.getInt("extra23")
                                        extra24 = jsonObject.getInt("extra24")
                                        extra25 = jsonObject.getInt("extra25")
                                        extra26 = jsonObject.getInt("extra26")
                                        extra27 = jsonObject.getInt("extra27")
                                        extra28 = jsonObject.getInt("extra28")
                                        extra29 = jsonObject.getInt("extra29")
                                        extra29 = jsonObject.getInt("extra29")
                                    } catch (e: JSONException) {
                                        e.printStackTrace()
                                    }
                                    TTT.set(null, "", data)
                                }
                            }
                            rv_video.scrollToPosition(0)
                        }
                    }
                } catch (e: Exception) {
                }
            }
        })
        when(0){
            // catch TTT
            1 ->{
                val jsonObject: JSONObject;
                var data:String? = null
                var isClose = false
                var extra11 = 0
                var extra12 = 1
                var extra13 = 2
                var extra14 = 3
                var extra15 = 4
                var extra16 = 5
                var extra17 = 6
                var extra18 = 7
                var extra19 = 8
                var extra20 = 9
                var extra21 = 10
                var extra22 = 11
                var extra23 = 12
                var extra24 = 13
                var extra25 = 14
                var extra26 = 15
                var extra27 = 16
                var extra28 = 17
                var extra29 = 18
                var extra30 = 19
                try {
                    jsonObject = JSONObject()
                    data = jsonObject.getString("data")
                    isClose = jsonObject.getBoolean("isClose")
                    extra11 = jsonObject.getInt("extra11")
                    extra12 = jsonObject.getInt("extra12")
                    extra13 = jsonObject.getInt("extra13")
                    extra14 = jsonObject.getInt("extra14")
                    extra15 = jsonObject.getInt("extra15")
                    extra16 = jsonObject.getInt("extra16")
                    extra17 = jsonObject.getInt("extra17")
                    extra18 = jsonObject.getInt("extra18")
                    extra19 = jsonObject.getInt("extra19")
                    extra20 = jsonObject.getInt("extra20")
                    extra21 = jsonObject.getInt("extra21")
                    extra22 = jsonObject.getInt("extra22")
                    extra23 = jsonObject.getInt("extra23")
                    extra24 = jsonObject.getInt("extra24")
                    extra25 = jsonObject.getInt("extra25")
                    extra26 = jsonObject.getInt("extra26")
                    extra27 = jsonObject.getInt("extra27")
                    extra28 = jsonObject.getInt("extra28")
                    extra29 = jsonObject.getInt("extra29")
                    extra29 = jsonObject.getInt("extra29")
                } catch (e: JSONException) {
                    e.printStackTrace()
                }
                TTT.set(null, "", data)
            }
        }
        viewModel.allVideoSavedRepostLiveData.observe(viewLifecycleOwner, ob2)

    }
}